package com.cjb.cloudsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjb.cloudsystem.entity.SysLogOperate;
import com.cjb.cloudsystem.mapper.SysLogOperateMapper;
import com.cjb.cloudsystem.service.SysLogOperateService;
import com.cjb.cloudsystem.vo.SysLogOperateVO;
import com.cjb.common.mybatisUtil.impl.BaseRepositoryImpl;
import com.cjb.common.util.Common;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @Author chenjinbin
* @Package IntelliJ IDEA
* @Class SysLogOperatesServiceImpl
* @Date 2023-07-27 21:41
*/
@Service
@AllArgsConstructor
public class SysLogOperateRepositoryImpl extends BaseRepositoryImpl<SysLogOperateMapper, SysLogOperate> implements SysLogOperateService {
/**
* 日志
*/
    private final static Logger log = LoggerFactory.getLogger(SysLogOperateRepositoryImpl.class);
    /*
    * 注入mapper层
    */
    private final SysLogOperateMapper sysLogOperateMapper;
    /**
     * 单体保存
     *
     * @return
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public boolean save(SysLogOperate sysLogOperate) {
        baseMapper.insert(sysLogOperate);
        return true;
    }

    /**
    *批量保存
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public Integer listSave(List<SysLogOperate> sysLogOperate) {
        return sysLogOperateMapper.insertBatchSomeColumn(sysLogOperate);
    }

    /**
    * 批量更新
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public void listUpdate(List<SysLogOperate> sysLogOperate) {
        sysLogOperateMapper.insertBatchSomeColumn(sysLogOperate);
    }

    /**
    * 批量删除
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public void listDelete(List<SysLogOperate> sysLogOperate) {
        List<Long> collect = sysLogOperate.stream().map(SysLogOperate::getId)
        .distinct()
        .collect(Collectors.toList());
        sysLogOperateMapper.deleteBatchIds(collect);
    }
    /**
    * 通过id删除
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public int deleteById(SysLogOperate sysLogOperate) {
    return sysLogOperateMapper.deleteById(sysLogOperate.getId());
    }

    /**
    *单个查询
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public SysLogOperate queryById(SysLogOperate sysLogOperate) {
        return  sysLogOperateMapper.selectById(sysLogOperate.getId());
    }

    @Override
    public Page queryPage(Page sysLogOperatePage , SysLogOperateVO sysLogOperate) {
        return sysLogOperateMapper.selectPage(sysLogOperatePage, getWrapper(sysLogOperate));
    }
    /**
     *查询属性
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    private LambdaQueryWrapper<SysLogOperate> getWrapper(SysLogOperateVO query) {
        LambdaQueryWrapper<SysLogOperate> wrapper = Wrappers.lambdaQuery();
        if(Common.isNotNull(query.getId())){wrapper.eq(SysLogOperate::getId,query.getId());}
        if(Common.isNotNull(query.getModule())){wrapper.eq(SysLogOperate::getModule,query.getModule());}
        if(Common.isNotNull(query.getName())){wrapper.eq(SysLogOperate::getName,query.getName());}
        if(Common.isNotNull(query.getReqUri())){wrapper.eq(SysLogOperate::getReqUri,query.getReqUri());}
        if(Common.isNotNull(query.getReqMethod())){wrapper.eq(SysLogOperate::getReqMethod,query.getReqMethod());}
        if(Common.isNotNull(query.getReqParams())){wrapper.eq(SysLogOperate::getReqParams,query.getReqParams());}
        if(Common.isNotNull(query.getIp())){wrapper.eq(SysLogOperate::getIp,query.getIp());}
        if(Common.isNotNull(query.getAddress())){wrapper.eq(SysLogOperate::getAddress,query.getAddress());}
        if(Common.isNotNull(query.getUserAgent())){wrapper.eq(SysLogOperate::getUserAgent,query.getUserAgent());}
        if(Common.isNotNull(query.getOperateType())){wrapper.eq(SysLogOperate::getOperateType,query.getOperateType());}
        if(Common.isNotNull(query.getDuration())){wrapper.eq(SysLogOperate::getDuration,query.getDuration());}
        if(Common.isNotNull(query.getStatus())){wrapper.eq(SysLogOperate::getStatus,query.getStatus());}
        if(Common.isNotNull(query.getUserId())){wrapper.eq(SysLogOperate::getUserId,query.getUserId());}
        if(Common.isNotNull(query.getRealName())){wrapper.eq(SysLogOperate::getRealName,query.getRealName());}
        if(Common.isNotNull(query.getResultMsg())){wrapper.eq(SysLogOperate::getResultMsg,query.getResultMsg());}
        if(Common.isNotNull(query.getTenantId())){wrapper.eq(SysLogOperate::getTenantId,query.getTenantId());}
        if(Common.isNotNull(query.getCreateTime())){wrapper.eq(SysLogOperate::getCreateTime,query.getCreateTime());}
        wrapper.orderByDesc(SysLogOperate::getId);
        return wrapper;
    }

    /**
    * 返回全部信息
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public List<SysLogOperate> returnAllData() {
        return sysLogOperateMapper.selectList(null);
    }

    /**
     * 导出操作
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public List<SysLogOperate> queryList(SysLogOperateVO sysLogOperate) {
        return sysLogOperateMapper.selectList(getWrapper(sysLogOperate));
    }

}